package com.iphone.extra;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.widget.TextView;

import com.iphone.extra.view.IWidgetFrameLayout;
import com.iphone.extra.view.ScreenTimeView;

import java.util.Calendar;

public class WidgetScreenTimeLayout extends IWidgetFrameLayout {

    private TextView mTotalTimeText;
    private ScreenTimeView mScreenTimeView;

    private Calendar mCalendar;
    private boolean isScreenOn = true;
    private final SparseIntArray mEveryHourArray = new SparseIntArray();

    public WidgetScreenTimeLayout(Context context) {
        this(context, null, 0);
    }

    public WidgetScreenTimeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WidgetScreenTimeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mCalendar = Calendar.getInstance();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTotalTimeText = (TextView) findViewById(R.id.screen_time_total_time);
        mScreenTimeView = (ScreenTimeView) findViewById(R.id.screen_time_view);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_TIME_TICK);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        getContext().registerReceiver(mReceiver, filter);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        getContext().unregisterReceiver(mReceiver);
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            if (Intent.ACTION_SCREEN_ON.equals(action)) {
                isScreenOn = true;
            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {
                isScreenOn = false;
            } else if (Intent.ACTION_TIME_TICK.equals(action)) {
                if (isScreenOn) {
                    mCalendar.setTimeInMillis(System.currentTimeMillis());
                    int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
                    mEveryHourArray.put(hour, mEveryHourArray.get(hour) + 1);
                    if (mScreenTimeView != null) {
                        mScreenTimeView.updateScreenTime(hour, mEveryHourArray);
                    }
                }
            } else if (Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
                mCalendar = Calendar.getInstance();
            }
        }
    };

    @Override
    public boolean onCellClicked(String describe) {
        return true;
    }

    @Override
    public String getLabel(Resources res) {
        return res.getString(R.string.widget_label_screen_time);
    }
}
